package com.example.music.activity; // 包名改为activity

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.example.music.R;
import com.example.music.fragment.CategoryFragment;
import com.example.music.fragment.SongsFragment;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

public class LocalMusicActivity extends AppCompatActivity { // 改为继承AppCompatActivity

    private ViewPager2 viewPager;
    private TabLayout tabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_local_music); // 使用新的布局文件

        // 初始化ViewPager2和TabLayout
        viewPager = findViewById(R.id.view_pager);
        tabLayout = findViewById(R.id.tab_layout);

        // 设置Adapter
        LocalMusicPagerAdapter adapter = new LocalMusicPagerAdapter(this);
        viewPager.setAdapter(adapter);

        // 绑定TabLayout与ViewPager2
        new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> {
            switch (position) {
                case 0: tab.setText("单曲"); break;
                case 1: tab.setText("专辑"); break;
                case 2: tab.setText("歌手"); break;
                case 3: tab.setText("文件夹"); break;
            }
        }).attach();

        viewPager.setCurrentItem(0); // 默认选中单曲Tab
    }

    // ViewPager2的Adapter
    private static class LocalMusicPagerAdapter extends FragmentStateAdapter {
        public LocalMusicPagerAdapter(@NonNull AppCompatActivity activity) {
            super(activity);
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            switch (position) {
                case 0: return SongsFragment.newInstance(SongsFragment.SortType.BY_SONG);
                case 1: return CategoryFragment.newInstance(CategoryFragment.CategoryType.BY_ALBUM);
                case 2: return CategoryFragment.newInstance(CategoryFragment.CategoryType.BY_ARTIST);
                case 3: return CategoryFragment.newInstance(CategoryFragment.CategoryType.BY_FOLDER);
                default: throw new IllegalArgumentException();
            }
        }

        @Override
        public int getItemCount() {
            return 4;
        }
    }
}